import json
import re
from .utils import get_page
from pyquery import PyQuery as pq


class ProxyMetaclass(type):
    def __new__(cls, name, bases, attrs):
        count = 0
        attrs['__CrawlFunc__'] = []
        for k, v in attrs.items():
            if 'crawl_' in k:
                attrs['__CrawlFunc__'].append(k)
                count += 1
        attrs['__CrawlFuncCount__'] = count
        return type.__new__(cls, name, bases, attrs)


class Crawler(object, metaclass=ProxyMetaclass):
    def get_proxies(self, callback):
        proxies = []
        for proxy in eval("self.{}()".format(callback)):
            print('成功获取到代理', proxy)
            proxies.append(proxy)
        return proxies

    def crawl_xiguadaili(self):
        url = "http://api3.xiguadaili.com/ip/?tid=555761992818874&num=100&delay=3&category=2"

        html = get_page(url)

        if html:
            with open("xigua_text", 'w', encoding="utf-8") as f:
                f.write(html)

                proxies = open("xigua_text")

            for proxy in proxies:
                yield proxy
